package com.ruoyi.quartz.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ruoyi.project.domain.OrderDispatch;
import com.ruoyi.project.domain.OrderPreOrder;
import com.ruoyi.project.mapper.OrderPreOrderMapper;
import com.ruoyi.project.service.IOrderDispatchService;

/**
 * 定时任务-项目经理超过两周未派单，自动派单给自己
 * 
 * @author he
 */
@Component("dispatchProjectTask")
public class DispatchProjectTask{
	@Autowired
	private OrderPreOrderMapper orderPreOrderMapper;
	@Autowired
	private IOrderDispatchService orderDispatchService;

	public void dispatchProject(){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("createTime", new Date());
		paramMap.put("status", "0");
		List<Map<String, Object>> orderPreOrderList = orderPreOrderMapper.getPreOrderList(paramMap);
		if(orderPreOrderList != null && orderPreOrderList.size() > 0){
			for(Map<String, Object> map : orderPreOrderList){

				// 插入派单表
				OrderDispatch od = new OrderDispatch();
				Date now = new Date();
				od.setOrderName(map.get("contractName") + "-" + map.get("processName"));
				od.setConProcessId(Long.parseLong(map.get("processId").toString()));
				od.setDispatchUserId(Long.parseLong(map.get("dispatchUserId").toString()));
				od.setDispatchUserName(map.get("dispatchUserName").toString());
				od.setDispatchTime(now);
				od.setReceiveUserId(Long.parseLong(map.get("dispatchUserId").toString()));
				od.setReceiveUserName(map.get("dispatchUserName").toString());
				od.setCreateBy(map.get("dispatchUserName").toString());
				od.setCreateTime(now);
				od.setCollectibleRatio(Double.parseDouble(map.get("collectibleRatio").toString()));
				od.setPreOrderId(Long.parseLong(map.get("preId").toString()));
				od.setOrderStatus("0");
				int a = orderDispatchService.insertOrderDispatch(od);
				// 修改可派单表状态
				if(a > 0){
					OrderPreOrder opo = new OrderPreOrder();
					opo.setId(Long.parseLong(map.get("preId").toString()));
					opo.setStatus("1");
					opo.setCollectibleRatio(Double.parseDouble(map.get("collectibleRatio").toString()));
					orderPreOrderMapper.updateOrderPreOrder(opo);
				}
			}
		}

	}

}
